package com.my.biz.service.impl;

import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.Validate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.my.api.response.Msg;
import com.my.biz.dao.CodeMapper;
import com.my.biz.domain.CodeDO;
import com.my.biz.service.ICodeService;
import com.my.biz.service.ISysConfigService;
import com.my.core.base.dao.GenericDao;
import com.my.core.base.service.AbstractGenericService;
import com.my.util.MailUtil;
import com.my.util.MsgUtil;
import com.my.util.RandomUtil;


 /**
 * 
 * @描述：ICodeService 服务实现类
 * 
 * @创建人： ch
 * 
 * @创建时间：2017年07月05日 18:07:39
 * 
 */
@Service("iCodeService")
public class CodeServiceImpl extends AbstractGenericService<CodeDO, Long> implements ICodeService{
	
	private static final Logger LOGGER = LoggerFactory.getLogger(CodeServiceImpl.class);
	
	@Autowired
	private ISysConfigService configService;
	
	@Autowired
	private CodeMapper codeMapper;
	
	@Autowired
	private MailUtil mailUtil;
	
	@Override
    public GenericDao<CodeDO, Long> getDao() {
        return codeMapper;
    }

	@Transactional(rollbackFor=Exception.class)
	@Override
	public Msg sendRegCodeEMailCode(String email) {
		CodeDO c = new CodeDO(email+"_mail");
		c = selectOne(c);
		Long curTime = System.currentTimeMillis();
		String code = null;
		if(c==null){
			Long expire_time = configService.getLongValueByKey("mail_code_time_out_time")+curTime;
			int codeLength = configService.getIntegerValueByKey("mail_code_length");
			code = RandomUtil.randomAll(codeLength);
			insert(new CodeDO(email+"_mail",code,expire_time));
		}else{
			Long time_out_time = configService.getLongValueByKey("mail_code_time_out_time");
			Integer send_count_limit = configService.getIntegerValueByKey("mail_code_send_count_limit");
			if(c.getSendCount()>=send_count_limit){
				Long free_time = c.getUpdateTime().getTime() + time_out_time;
				if(free_time > curTime){
					Long time = (free_time - curTime )/ 60000l;
					return MsgUtil.error(String.format("多次发送邮件,请稍等%s分钟后再发送", time==0?1:time));
				}
			}
			Long expire_time = time_out_time+curTime;
			int codeLength = configService.getIntegerValueByKey("mail_code_length");
			code = RandomUtil.randomAll(codeLength);
			update(new CodeDO(code, expire_time, c.getSendCount() + 1), new CodeDO(email+"_mail"));
		}
		
		Validate.isTrue(mailUtil.sendRegCodeQQEMail(email, code)  ,"发送邮件失败");
		return MsgUtil.success("发送成功");
		
	}

	@Override
	public Boolean checkCode(String email, String code) {
		CodeDO c = new CodeDO(email,code);
		c = selectOne(c);
		Validate.notNull(c,"验证码不存在");
		Validate.isTrue(c.getExpireTime()==0l || c.getExpireTime() > System.currentTimeMillis()  ,"验证码过期");
		Validate.isTrue(ObjectUtils.equals(c.getCode(),code),"验证码不匹配");
		return Boolean.TRUE;
	}

	@Override
	public Boolean addLoginCode(String email, String code) {
		CodeDO c = new CodeDO(email+"_captcha");
		c = selectOne(c);
		if(c!=null)removeLoginCode(email);
		return insert(new CodeDO(email+"_captcha",code,0l))>0;
	}

	@Override
	public Boolean removeLoginCode(String email) {
		return deleteByCondition(new CodeDO(email+"_captcha"))>0;
	}
    


}
